package com.hevery.crud.controller;

import com.hevery.crud.bean.EasyUIResult;
import com.hevery.crud.pojo.User;
import com.hevery.crud.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;

/**
 * Created by zhy on 2017/8/23.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public EasyUIResult queryUserList(@RequestParam("page")Integer page,@RequestParam("rows")Integer rows){
        EasyUIResult easyUIResult = userService.queryUserList(page,rows);
        return  easyUIResult;
    }

    /**
     * 根据用户Id查询信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "{userId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> queryUserById(@PathVariable Long userId){
        try {
            User user = userService.queryUserById(userId);
            System.out.println(user);
            if(null == user){
                //如果资源不存在响应404
                System.out.println("404----->"+user);
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            //return ResponseEntity.status(HttpStatus.OK).body(user);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //服务器内部异常
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }

    /**
     *新增用户
     * @param user
     * @return
     */
     @RequestMapping(method = RequestMethod.POST)
     public ResponseEntity<Void> saveUser(User user){
         try {
             Boolean bool = userService.saveUser(user);
             if(bool){
                 //新增成功响应201
                 return ResponseEntity.status(HttpStatus.CREATED).build();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         //服务器内部异常
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
     }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user){
        try {
            Boolean bool = userService.updateUser(user);
            if(bool){
                //更新成功，响应204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //服务器内部异常
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteUser(@RequestParam(value= "id" ,defaultValue = "0") Long id){
        try {
            if(id.longValue() == 0){
                //没有参数传递响应400,参数列表错误
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            Boolean bool = userService.deleteUser(id);
            if(bool){
                //删除成功,响应204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //删除失败，响应500，服务器内部异常
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
